let app = getApp();
let compose = require('../../utils/compose');
let dateFormat = require('../../utils/dateutil');
let utils = require('../../utils/utils');
let productSearchParams;
var oneDay = 1 * 24 * 60 * 60 * 1000;
Page({
  data: {

    loading: true,

  },

  onLoad: function () {//加载时获取请求的参数
    //加载前设置loading为true
    this.setData({
      loading: true
    });
    //得到查询参数
    productSearchParams = wx.getStorageSync('productSearchParams');
    //查询车次信息
    var that = this;

    app.get('/BusTicketInfo.go', {
      method: 'queryTicketInfo',
      proxyId: app.globalData.proxyid,
      query: '',
      type: "web",
      sellstationid: productSearchParams.StartCity.id,
      startName: productSearchParams.StartCity.name,
      endName: productSearchParams.EndCity.name,
      pszBusdate: productSearchParams.GoDate

    }).then(function (data) {
      if (data.totalCount > 0) {

        that.setData({

          businfo: data.rows,
          startdate: productSearchParams.GoDate
        })
      }
      that.setData({
        loading: false
      })
    }).catch(function (e) {
      console.log(e)
    })

  },


  // 前一天
  before: function () {
    //查询日期
    var queryday = new Date(this.data.startdate);
    queryday = queryday.valueOf() - oneDay;
    var beforeday = new Date(queryday);
    //当前日期
    var today = new Date(Date.now());
    var bv = dateFormat.compareDate(dateFormat.formatDay(today), dateFormat.formatDay(beforeday));
    if (bv == true) {
      this.setData({
        startdate: dateFormat.formatDay(beforeday)
      });
      Query(dateFormat.formatDay(beforeday), this);
    }
    else {
      this.setData({
        startdate: dateFormat.formatDay(today)
      })
      Query(dateFormat.formatDay(today), this);
    }
  },
  // 后一天
  next: function () {

    //最大查询日期
    var n = app.globalData.selldays;
    var max = dateFormat.formatDay(new Date(Date.now() + n * oneDay));

    //查询日期
    var queryday = new Date(this.data.startdate);
    queryday = queryday.valueOf() + oneDay;
    var nextday = new Date(queryday);
    //当前日期
    var today = new Date(Date.now());
    var bv = dateFormat.compareDate(max, dateFormat.formatDay(nextday));
    if (bv == true) {
      this.setData({
        startdate: max
      });
      Query(max, this);

    }
    else {
      this.setData({
        startdate: dateFormat.formatDay(nextday)
      });
      Query(dateFormat.formatDay(nextday), this);
    }
  }
})

function Query(date, data) {

  //得到查询参数
  productSearchParams = wx.getStorageSync('productSearchParams');
  //查询车次信息
  var that = data;

  app.get('/BusTicketInfo.go', {
    method: 'queryTicketInfo',
    proxyId: app.globalData.proxyid,
    query: '',
    type: "web",
    sellstationid: productSearchParams.StartCity.id,
    startName: productSearchParams.StartCity.name,
    endName: productSearchParams.EndCity.name,
    pszBusdate: date

  }).then(function (data) {
    if (data.totalCount > 0) {

      that.setData({

        businfo: data.rows

      })
    }
    that.setData({
      loading: false
    })
  }).catch(function (e) {
    console.log(e)
  })

}